#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<iostream>
#include<fstream>

using namespace std;
void printLine (int n, int m) {

    for (int i = 0; i < n; i++){  
        printf("|");
        for (int j = 0; j < m; j++) {
            printf("---|");
        }
        printf("\n");
    }
    
}
 
void printTable (char table[3][3]){

    printLine (1, 3);
    for (int i = 0; i < 3; i++) {
        printf ("|");
        for (int j = 0; j < 3; j++) {
            printf (" %c ", table[i][j]);
            printf ("|");
        }    
        printf ("\n");
        printLine (1, 3);
    }
}

void Proverka(char table[3][3], bool &was) {
    
    if ((table[0][0] == table[1][1]) && (table[1][1] == table[2][2]) && (table[0][0] == 'x')) {
        printf("Выиграл %c\n", table[0][0]);
        was = true;
    }
    
    if ((table[0][0] == table[1][1]) && (table[1][1] == table[2][2]) && (table[0][0] == 'o')) {
        printf("Выиграл %c\n", table[0][0]);
        was = true;
    }
    
    if ((table[2][0] == table[1][1]) && (table[1][1] == table[0][2]) && (table[2][0] == 'x')) {
        printf("Выиграл %c\n", table[2][0]);
        was = true;
    }
    
    if ((table[2][0] == table[1][1]) && (table[1][1] == table[0][2]) && (table[2][0] == 'o')) {
        printf("Выиграл %c\n", table[2][0]);
        was = true;
    }
    
    for (int i = 0; i < 3; i++) {
        if ((table[i][0] == table[i][1]) && (table[i][1] == table[i][2]) && (table[i][0] == 'x')) {
            printf("Выиграл %c\n", table[i][0]);
            was = true;
            break;
        }
    }
    
    for (int i = 0; i < 3; i++) {
        if ((table[i][0] == table[i][1]) && (table[i][1] == table[i][2]) && (table[i][0] == 'o')) {
            printf("Выиграл %c\n", table[i][0]);
            was = true;
            break;
        }
    }
    
    for (int j = 0; j < 3; j++) {
        if ((table[0][j] == table[1][j]) && (table[1][j] == table[2][j]) && (table[0][j] == 'x')) {
            printf("Выиграл %c\n", table[0][j]);
            was = true;
            break;
        }
    }
    
    for (int j = 0; j < 3; j++) {
        if ((table[0][j] == table[1][j]) && (table[1][j] == table[2][j]) && (table[0][j] == 'o')) {
            printf("Выиграл %c\n", table[0][j]);
            was = true;
            break;
        }
    }

}

void Xod1(char table[3][3], char mys) {
    int save;
    char path[100];
    char name[100] ;
    char fullname[300];
    strcpy(path, "home/school/projects/save/");
    FILE *file;
    
    int x = 0, y = 0;
    cout<<"Введите координаты хода : "<<endl;
    cin>>x;
    cin>>y;
    if ((x == -1) && (y == -1)){
        cout<<"Имя сохранённой игры : ";
        cin>>name;
        //strcpy(fullname, path);
        strcat(fullname, name);
        file = fopen (fullname, "wb");
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 3; j++){
                fwrite (&table[i][j], sizeof(char), 1, file);
            }
        }
        fwrite (&mys, sizeof(char), 1, file);
    } else {
        table[x][y] = mys;
    }
}

void Xodc(char table[3][3], char rob, char mys, int &count) { 

    bool done = false;
    
    if (!done && (table[1][1] == ' ')){
        table[1][1] = rob;
        done = true;
    }
    
    if (!done && (table[0][0] == ' ') && (table[2][2] != mys)){
        table[0][0] = rob;
        done = true;
    }
    
    if (!done && (table[0][2] == ' ') && (table[2][0] != mys) && (table[2][2] != mys)){
        table[0][2] = rob;
        done = true;
    }
    
    
                
    if (!done && (table[0][0] == table[1][1]) && (table[1][1] != table[2][2]) && (table[2][2] == ' ')) {
        table[2][2] = rob;
        done = true;
    }

    if (!done && (table[0][0] != table[1][1]) && (table[0][0] == table[2][2]) && (table[1][1] == ' ')) {
        table[1][1] = rob;
        done = true;
    }


    if (!done && (table[0][0] != table[1][1]) && (table[1][1] == table[2][2]) && (table[0][0] == ' ')) {      
        table[0][0] = rob;
        done = true;
    }

    if (!done && (table[2][0] == table[1][1]) && (table[1][1] != table[0][2]) && (table[0][2] == ' ')) {
        table[0][2] = rob;
        done = true;
    }

    if (!done && (table[2][0] != table[1][1]) && (table[1][1] == table[0][2]) && (table[2][0] == ' ')) {
        table[2][0] = rob;
        done = true;
    }

    if (!done && (table[2][0] == table[0][2]) && (table[1][1] != table[0][2]) && (table[1][1] == ' ')) {
        table[1][1] = rob;
        done = true;                    
    }
    
    

    for (int i = 0; i < 3; i++) {
        if (!done && (table[i][0] == table[i][1]) && (table[i][1] != table[i][2]) && (table[i][2] == ' ')) {
            table[i][2] = rob;
            done = true;
        }
                                 
        if (!done && (table[i][0] == table[i][2]) && (table[i][1] != table[i][2]) && (table[i][1] == ' ')) {
            table[i][1] = rob;
            done = true;
        }
            
        if (!done && (table[i][0] != table[i][1]) && (table[i][1] == table[i][2]) && (table[i][0] == ' ')) {
            table[i][0] = rob; 
            done = true;
        }
    }
        
    for (int j = 0; j < 3; j++) {
        if (!done && (table[0][j] == table[1][j]) && (table[1][j] != table[2][j]) && (table[2][j] == ' ')) {
            table[2][j] = rob;
            done = true;
        }
            
        if (!done && (table[0][j] == table[2][j]) && (table[1][j] != table[2][j]) && (table[1][j] == ' ')) {
            table[1][j] = rob;
            done = true;
        }
            
        if (!done && (table[0][j] != table[1][j]) && (table[1][j] == table[2][j]) && (table[0][j] == ' ')) {
            table[0][j] = rob;
            done = true;
        }
    }
    
    
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            if (!done && (table[i][j] == ' ')){
                table[i][j] = rob;
                done = true;
                break;
            }
        }
    }        

 
    printf("\n");
}


int main()  { 
    // Координаты ввода
    int x = 0;
    int y = 0; 
    char str[100]; // Символ человека
    char mys;
    char rob; // Символ компьютера
    int count = 9; // Счетчик ходов 
    //Создать пустое поле пробелов
    char table[3][3];
    bool was = false;
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            table[i][j] = ' '; 
            
        }
    }
    
   // Соханение
    int save;
    char path[100];
    char name[100] ;
    char fullname[300];

    strcpy(path, "home/school/projects/save/");

    FILE *file;
    cout<<("Загрузить сохраннёную игру?(0-нет,1-да): ");
    cin>>save;
    if (save == 1){
        printf("Имя сохранённой игры : ");
        scanf("%s", name);
        strcpy(fullname, path);
        strcat(fullname, name);
        file = fopen (fullname, "br");
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 3; j++){
                fread (&table[i][j], sizeof(char), 1, file);
            }
        }
    }    
    cout<<"Для сохранение игры наберите -1 -1"<<endl;
    cout<<("Выберите символ (x или o): ");
    cin>> str[0];
    mys = str[0];
        
    if (mys == 'x'){
        rob = 'o';
    } else {
        rob = 'x';
    }

    while (count > 0) {
        if (was) {
            break;
        }
        if (mys == 'x'){
            Xod1(table, mys);
            count--;
            printTable(table);
            Proverka(table, was);
            if ((count == 0)||(was == true)) {
                break;
            }
            Xodc(table, rob, mys, count);
            count--;
            printTable(table);
            Proverka(table, was);
        } else {
            Xodc(table, rob, mys, count);
            count--;
            printTable(table);
            Proverka(table, was);
            if ((count == 0)||(was == true)) {
                break;
            }
            Xod1(table, mys);
            count--;
            printTable(table);
            Proverka(table, was);
        }               
    }
    printf("Игра окончена\n");
    
    return 0;
} 
